/* 
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.  
 */
package erwiki.part.rssgenerator.internal;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.elwiki_data.AttachmentContent;
import org.elwiki_data.PageAttachment;
import org.elwiki_data.WikiPage;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import erwiki.api.Release;
import erwiki.api.attachment.AttachmentManager;
import erwiki.api.core.ContextEnum;
import erwiki.api.core.Engine;
import erwiki.api.core.WikiContext;
import erwiki.api.exceptions.ProviderException;
import erwiki.api.rss.IEntry;
import erwiki.api.rwc.RWC;
import erwiki.configuration.IWikiConfiguration;

/**
 *  Provides an Atom 1.0 standard feed, with enclosures.
 *
 */
public class AtomFeed extends Feed {

    private Namespace m_atomNameSpace = Namespace.getNamespace("http://www.w3.org/2005/Atom");

    /** Defines a SimpleDateFormat string for RFC3339-formatted dates. */
    public static final String RFC3339FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZ";

    /**
     *   This is a bit complicated right now, as there is no proper metadata store in JSPWiki.
     *
     *   @return An unique feed ID.
     */
    private String getFeedID()
    {
    	Engine engine = RWC.INSTANCE.getEngine();
        return engine.getWikiConfiguration().getBaseURL(); // FIXME: This is not a feed id
    }

    private String getEntryID( final IEntry e )
    {
        return e.getURL(); // FIXME: Not really a feed id!
    }

    private Collection<Element> getItems() {
        final ArrayList< Element > list = new ArrayList<>();
        final Engine engine = RWC.INSTANCE.getEngine();
        AttachmentManager attachmentManager = engine.getManager(AttachmentManager.class);
        ServletContext servletContext = null;
        HttpServletRequest httpRequest = RWC.INSTANCE.getHttpRequest();
        if( httpRequest != null ) {
            servletContext = httpRequest.getSession().getServletContext();
        }

        for( final IEntry e : m_entries ) {
            final WikiPage p = e.getPage();
            final Element entryEl = getElement( "entry" );

            //  Mandatory elements
            entryEl.addContent( getElement( "id" ).setText( getEntryID( e ) ) );
            entryEl.addContent( getElement( "title" ).setAttribute( "type", "html" ).setText( e.getTitle() ) );
            entryEl.addContent( getElement( "updated" ).setText( DateFormatUtils.formatUTC( p.getLastModifiedDate(), RFC3339FORMAT ) ) );

            //  Optional elements
            entryEl.addContent( getElement( "author" ).addContent( getElement( "name" ).setText( e.getAuthor() ) ) );
            entryEl.addContent( getElement( "link" ).setAttribute( "rel", "alternate" ).setAttribute( "href", e.getURL() ) );
            entryEl.addContent( getElement( "content" ).setAttribute( "type", "html" ).setText( e.getContent() ) );

            //  Check for enclosures
            if( attachmentManager.hasAttachments( p ) && servletContext != null ) {
                try {
                    final List< PageAttachment > c = attachmentManager.listAttachments( p );
                    for( final PageAttachment att : c ) {
                        final Element attEl = getElement( "link" );
                        attEl.setAttribute( "rel", "enclosure" );
                        attEl.setAttribute( "href", engine.getURL( WikiContext.ATTACHMENT_DOGET, att.getName(), null ) );
						AttachmentContent attContent = att.forLastContent();
						long size = (attContent != null) ? attContent.getSize() : -1;
						attEl.setAttribute("length", Long.toString(size));
	                      //:FVK: attEl.setAttribute( "type", getMimeType( servletContext, att.getFileName() ) );

                        entryEl.addContent( attEl );
                    }
                } catch( final ProviderException ex ) {
                    // FIXME: log.info("Can't get attachment data",ex);
                }
            }

            list.add( entryEl );
        }

        return list;
    }

    /**
     *  {@inheritDoc}
     */
    @Override
    public String getString() {
        final Element root = getElement("feed");
        Engine engine = RWC.INSTANCE.getEngine();
        IWikiConfiguration config = engine.getWikiConfiguration();

        Date lastModified = new Date(0L);

        for( final IEntry e : m_entries ) {
            if( e.getPage().getLastModifiedDate().after( lastModified ) ) {
                lastModified = e.getPage().getLastModifiedDate();
            }
        }

        //  Mandatory parts
        root.addContent( getElement( "title" ).setText( getChannelTitle() ) );
        root.addContent( getElement( "id" ).setText( getFeedID() ) );
        root.addContent( getElement( "updated" ).setText( DateFormatUtils.formatUTC( lastModified, RFC3339FORMAT ) ) );

        //  Optional
        // root.addContent( getElement("author").addContent(getElement("name").setText(format())))
        root.addContent( getElement( "link" ).setAttribute( "href", config.getBaseURL() ) );
        root.addContent( getElement( "generator" ).setText( "JSPWiki " + Release.VERSTR ) );

        String rssFeedURL = "";
		try {
			;
			String pageName = Optional.ofNullable(RWC.INSTANCE.getPageName()).orElse("__DUMMY");
			rssFeedURL = engine.getURL(WikiContext.PAGE_NONE, "rss.jsp", //
					"page=" + engine.encodeName(pageName) + //
							"&mode=" + m_mode + //
							"&type=atom");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        final Element self = getElement( "link" ).setAttribute( "rel","self" );
        self.setAttribute( "href", rssFeedURL );
        root.addContent( self );

        //  Items
        root.addContent( getItems() );

        //  aaand output
        final XMLOutputter output = new XMLOutputter();
        output.setFormat( Format.getPrettyFormat() );

        try {
            final StringWriter res = new StringWriter();
            output.output( root, res );

            return res.toString();
        } catch( final IOException e ) {
            return null;
        }
    }

    private Element getElement( final String name ) {
        return new Element( name, m_atomNameSpace );
    }

}
